﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: TODO.
\**************************************************************************************************/

import arkadium.SoundClip;
import arkadium.SoundManager;
import arkadium.GUIManager;
import arkadium.AppConfigFile;
import arkadium.EncryptionManager;
import arkadium.EventManager;
import arkadium.SharedObjectManager;
import game.Actor;
import game.Common;
import game.GUI;
import game.Session;
import mx.data.encoders.Num;
import mx.utils.Delegate;


class game.Main extends arkadium.Main
{
	public var _sFrameworkSetting:String
	public var _cEncryptionManager:EncryptionManager;
	public var _cEventManager:EventManager;
	public var _cSoundManager:SoundManager;
	public var _cAppConfigFile:AppConfigFile;
	public var _cSharedObjectManager:SharedObjectManager;
	public var _cGUIManager:GUIManager;
	public var _smcMyStage:MovieClip
	public var _cGUI:GUI;
	private var _aLoop:Array;
	
	
	
	
	private static var _cInstance:Main = null;
	
	public static function getInstance():Main
	{
		if ( !Main._cInstance )
		{
			_cInstance = new Main();
		}
		
		return Main._cInstance;
	}
	
	private function Main()
	{
		
	}
	
	public function update():Void {
		for ( var i:Number = 0; i < _aLoop.length; i++ ) {
			Actor(_aLoop[i]).update();
		}
	}
	
	public function removeFromLoop( object: Object) {
		Common.removeMeFrom( object, _aLoop );
	}
	
	public function clearLoop():Void {
		_aLoop = new Array();
	}
	
	public function addToLoop(actor:Actor) {
		_aLoop.push(actor);
	}
	
	public function onInitialize( mcStage:MovieClip )
	{
		_smcMyStage = mcStage;
		_smcMyStage.onEnterFrame = Delegate.create( this, update )
		
		_aLoop = new Array();
		
		super.onInitialize( _smcMyStage, true );
		
		//init the eventManager.  All of this games elements will be able to communicate through this event manager
		_cEventManager = new EventManager();
		
		//init the shared object if you want.  Make sure to initialize it with a new shared object name
		_cSharedObjectManager = new SharedObjectManager()
		_cSharedObjectManager.onInitialize("template_sharedobject");
		
		//This initiates the sound manager for this game
		//it needs to be sent a variable that is the stage where the sounds will be attached
		//you can send the "eventManager" which will listen for sound on off events
		//finally you can send the "sharedobjectmanager" if you would like the sound manager to save the sound state
		_cSoundManager  = new SoundManager();
		_cSoundManager.onInitialize(_smcMyStage,_cEventManager, _cSharedObjectManager);
		
		
		//_cGUIManager = _smcMyStage._smcGUIManager;
		//calls a function to init all starting sounds
		initSounds();	
		
		//Damian
		_cAppConfigFile = new AppConfigFile(this);
		
		
		//List the plugins you would like to use in this initialization of the encryption manger
		//then choose the encryption that you would like to use by default.
		//Availible encryptions are "TEA"
		_cEncryptionManager = new EncryptionManager ();
		_cEncryptionManager.onInitialize(["TEA","MD5"],"TEA");
		
		//These are required only for GDG games
				
		//getting initial setup variables	
		game.StatusMessagePanel.getInstance().showLoadingMessage( "Loading Config File" );
		
		//setting up the function for receiving flash vars etc
		if(_level0['params']!= undefined) {
			trace(_level0['params']);
			_cAppConfigFile.setConfigDataFlv(_level0);
			_sFrameworkSetting = "gamecenter";
			game.StatusMessagePanel.getInstance().hideMessage();
			//_cGUIManager.onInitialize();
		}
		else if(_level0['endURL'] != undefined){
			
			_cAppConfigFile.setConfigDataFlv(_level0);
			_sFrameworkSetting = "gamecenter";
			game.StatusMessagePanel.getInstance().hideMessage();
			//_cGUIManager.onInitialize();
		}
		else {
			//This is the code for loading an xml config file 
			//AppConfigFile.getInstance().loadConfigFile( this, configFileLoaded, "ark_config.xml" );
			
			//here is a txt version, notice that the function call is the same
			_cAppConfigFile.loadConfigFile( this, configFileLoaded, "config_unencrypted.txt" );
		}
		
		
		if(_cAppConfigFile == undefined){
			_cGUIManager.onInitialize();
		}
	}
	
	public function initSounds(){
	   /* _cSoundManager.attachSoundInLibrary( "level1Music", true,100 );
		_cSoundManager.attachSoundInLibrary( "clickSound", false,100 );
		_cSoundManager.attachSoundInLibrary( "rolloverSound", false,100 );
		_cSoundManager.attachExternalSound( "pointUpSound","External_Sounds/golfBallInHole2.mp3", false,100 );
		_cSoundManager.attachExternalSound( "musicSound", "External_Sounds/easygoin4.mp3", true, 100 );
		*/
		//_cSoundManager.attachExternalSound( "gamePlay1", "External_Sounds/Spliterature_GamePlay_Mx_Short_10sec_01.wav", true, 100 );
		
		/*MUSIC */
		
		_cSoundManager.attachSoundInLibrary( "game1", true, 100 );
		_cSoundManager.attachSoundInLibrary( "game2", true, 100 );
		_cSoundManager.attachSoundInLibrary( "intro1", true, 100 );
		
		/*SOUNDS*/
		_cSoundManager.attachSoundInLibrary( "down", true, 100 );
		_cSoundManager.attachSoundInLibrary( "mousedown", false, 100 );
		_cSoundManager.attachSoundInLibrary( "mouseup", false, 100 );
		_cSoundManager.attachSoundInLibrary( "incorrect", false, 100 );
		_cSoundManager.attachSoundInLibrary( "correct", false, 100 );
		_cSoundManager.attachSoundInLibrary( "rollover", false, 100 );
		_cSoundManager.attachSoundInLibrary( "click", false, 100 );
		
				
	}
	
	public function configFileLoaded( bSuccess:Boolean )
	{
		if ( bSuccess )
		{
			
			_cAppConfigFile.testAndHandleDecryption();
			//game.StatusMessagePanel.getInstance().showSuccessMessage( "Success!" );
			game.StatusMessagePanel.getInstance().hideMessage();
			_sFrameworkSetting = "config";
			//_smcGUIManager.hidePanel("STATUS_MESSAGE_PANEL");
			_cGUIManager.onInitialize();
			_cGUI = new GUI( _mcStage );
			Session.getInstance().initialize();
		
		}
		else
		{
			game.StatusMessagePanel.getInstance().showSuccessMessage( "Fail!" );
			_sFrameworkSetting = "stolen";
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}